<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

class UI{
	public function __construct($type){
		if(file_exists(CORE_PATH."interface.ui.{$type}.php")){
			require_once(CORE_PATH."interface.ui.{$type}.php");
			$this->type = $type;
		}
		else{
			throwFatalError("Interface initialized does not exist! ({$type})");
		}
		$this->scontent = "";
	}
	
	/**
	 * startStyleFile
	 * Loads the style file specified.
	 * @param int id
	 * @return bool
	 */
	public function startStyleFile($id) {
		// Build the path.
		$path = DESIGN_PATH."styles/{$this->type}/id_{$id}/";
		if(file_exists($path."style.php")) {
			require_once($path."style.php");
			$this->Style = new Style;
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * message
	 * Outputs a single message. Might have to standardize this to 'Message', and use message as a placeholder.
	 * Returns the HTML if return param is true, otherwise return boolean false.
	 * @param string message
	 * @param string type="Regular"
	 * @param bool return
	 * @return mixed
	 */
	public function message($message, $type = "regular", $return = false) {
		global $Smarty;
		$Smarty->assign("message", $message);
		$Smarty->assign("msgid", rand(1,9999999));
		if(!$return) {
			$Smarty->display("message_{$type}.tpl");
			return true;
		}
		else return $Smarty->fetch("message_{$type}.tpl");
	}
	
	/**
	 * twoColumnLayout
	 * Generates a two-column table-based layout, for (probably) column'd dashboards like mine.
	 * Widths are specified as width='' attribute in HTML, so use 200px, 20%, etc. Returns HTML if return=true, otherwise return bool true
	 * @param string width1
	 * @param string width2
	 * @param string content1
	 * @param string content2
	 * @param bool return=false
	 */
	public function twoColumnLayout($width1, $width2, $content1, $content2, $return = false) {
		global $Smarty;
		$Smarty->assign("col1w", $width1);
		$Smarty->assign("col2w", $width2);
		$Smarty->assign("col1cont", $content1);
		$Smarty->assign("col2cont", $content2);
		if(!$return){
			$Smarty->display("2column.tpl");
			return true;
		}
		else return $Smarty->fetch("2column.tpl");
	}

	public function basicTable($title, $content, $icon = "silk/table.png", $padded = true) {
		global $Smarty;
		$Smarty->assign("btitle", $title);
		$Smarty->assign("bcont", $content);
		$Smarty->assign("bicon", $icon);
		if($padded):
			$Smarty->assign("bpadded", true);
		else:
			$Smarty->assign("bpadded", false);
		endif;
		$Smarty->assign("tablename", rand(1,9999999));
		return $Smarty->fetch("basicTable.tpl");
	}

	public function redirect($url, $seconds = 2, $internal = true, $message = false, $msgtype = false){
		global $Smarty;
		if(!$internal) $Smarty->assign("url", $url);
		else $Smarty->assign("url", WEB_PATH.$url);
		$Smarty->assign("seconds", $seconds);
		$Smarty->display("redirect.tpl");
		// Message?
		if($message !== false and $msgtype == false)
			$this->message($message);
		elseif($message !== false and $msgtype !== false)
			$this->message($message, $msgtype);
	}
	
	/*
	 * Backend ONLY, otherwise just silently drop :)
	 */
	function write_sidebar($content)
	{
		$this->scontent .= $content;
	}
	
	function write_sidebar_link($link, $title, $icon) {
		if(APP_TYPE == "backend") $this->write_sidebar($this->Style->sidebar_link($link, $title, $icon));
	}
	
	function write_sidebar_head($title) {
		if(APP_TYPE == "backend") $this->write_sidebar($this->Style->sidebar_head($title));
	}
}
